﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace WebStudio.Framework.Core
{
    public class SysVariable
    {

        // 字段

        private static string _ApplicationPath = "~";

        // 方法

        public static string AbsoluteToResolveApplicationPath(string absoluteApplicationPath)
        {
            if (ApplicationPath == "")
            {
                return absoluteApplicationPath.Substring(1);
            }
            return absoluteApplicationPath.Substring(ApplicationPath.Length + 1);
        }

        public static string ResolveApplicationPath(string baseApplicationPath)
        {
            return (ApplicationPath + "/" + baseApplicationPath);
        }

        public static string ResolveToAbsoluteApplicationPath(string resolveApplicationPath)
        {
            if (ApplicationPath == "")
            {
                return ("/" + resolveApplicationPath);
            }
            return (ApplicationPath + "/" + resolveApplicationPath);
        }

        // 属性

        public static string ApplicationPath
        {
            get
            {
                if (CurrentContext != null)
                {
                    _ApplicationPath = CurrentContext.Request.ApplicationPath.EndsWith("/") ? "" : CurrentContext.Request.ApplicationPath;
                }
                return _ApplicationPath;
            }
        }

        public static string ApplicationPathMap
        {
            get
            {
                if (CurrentContext == null)
                {
                    return "~";
                }
                if (!(ApplicationPath == ""))
                {
                    return CurrentContext.Server.MapPath(ApplicationPath);
                }
                return CurrentContext.Server.MapPath("/");
            }
        }

        public static string AppURLBase
        {
            get
            {
                return ("http://" + URLSuffix);
            }
        }

        public static HttpContext CurrentContext
        {
            get
            {
                return HttpContext.Current;
            }
        }

        public static Uri CurrentUrl
        {
            get
            {
                if ((CurrentContext != null) && (CurrentContext.Request.Url != null))
                {
                    return CurrentContext.Request.Url;
                }
                return new Uri("http://www.xderive.com");
            }
        }

        public static Uri CurrentUrlReferrer
        {
            get
            {
                if (CurrentContext == null)
                {
                    return new Uri("http://www.xderive.net");
                }
                if (CurrentContext.Request.UrlReferrer != null)
                {
                    return CurrentContext.Request.UrlReferrer;
                }
                return new Uri(AppURLBase);
            }
        }

        public static string URLBase
        {
            get
            {
                if (URLPort == 80)
                {
                    return ("http://" + URLHost);
                }
                return ("http://" + URLHost + ":" + URLPort.ToString());
            }
        }

        public static string URLHost
        {
            get
            {
                if (CurrentContext != null)
                {
                    return CurrentContext.Request.Url.Host;
                }
                return "www.xderive.com";
            }
        }

        public static int URLPort
        {
            get
            {
                if (CurrentContext != null)
                {
                    return CurrentContext.Request.Url.Port;
                }
                return 80;
            }
        }

        public static string URLSuffix
        {
            get
            {
                if (URLPort == 80)
                {
                    return (URLHost + ApplicationPath);
                }
                return (URLHost + ":" + URLPort.ToString() + ApplicationPath);
            }
        }

        public static string UserHostAddress
        {
            get
            {
                if (CurrentContext == null)
                {
                    return "127.0.0.1";
                }
                return CurrentContext.Request.UserHostAddress;
            }
        }
    }

}
